<!DOCTYPE html><html lang="zh-Hans"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"><meta name="description" content="yarn和npm"><meta name="keywords" content="yarn npm"><meta name="author" content="晟松"><meta name="copyright" content="晟松"><title>yarn和npm | 晟松</title><link rel="shortcut icon" href="/logo.png"><link rel="stylesheet" href="/css/index.css?version=1.9.0"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome@latest/css/font-awesome.min.css?version=1.9.0"><meta name="format-detection" content="telephone=no"><meta http-equiv="x-dns-prefetch-control" content="on"><link rel="dns-prefetch" href="https://cdn.jsdelivr.net"><link rel="dns-prefetch" href="https://hm.baidu.com"><script>var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?c34d81d53341febe06a155941e2df16d";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();</script><meta http-equiv="Cache-Control" content="no-transform"><meta http-equiv="Cache-Control" content="no-siteapp"><script>var GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容:${query}"}},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  hexoVersion: '5.4.0'
} </script><meta name="generator" content="Hexo 5.4.0"></head><body><i class="fa fa-arrow-right" id="toggle-sidebar" aria-hidden="true"></i><div id="sidebar" data-display="true"><div class="toggle-sidebar-info text-center"><span data-toggle="切换文章详情">切换站点概览</span><hr></div><div class="sidebar-toc"><div class="sidebar-toc__title">目录</div><div class="sidebar-toc__progress"><span class="progress-notice">你已经读了</span><span class="progress-num">0</span><span class="progress-percentage">%</span><div class="sidebar-toc__progress-bar"></div></div><div class="sidebar-toc__content"><ol class="toc"><li class="toc-item toc-level-5"><a class="toc-link" href="#1-%E5%BF%AB%E9%80%9F%E5%88%A0%E9%99%A4node-modules"><span class="toc-number">1.</span> <span class="toc-text">1.快速删除node_modules</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#2-yarn%E7%9A%84%E5%AE%89%E8%A3%85"><span class="toc-number">2.</span> <span class="toc-text">2.yarn的安装:</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#3-%E5%B8%B8%E7%94%A8"><span class="toc-number">3.</span> <span class="toc-text">3.常用</span></a></li></ol></div></div><div class="author-info hide"><div class="author-info__avatar text-center"><img src="/img/avatar.png"></div><div class="author-info__name text-center">晟松</div><div class="author-info__description text-center">晟松的博客</div><div class="follow-button"><a target="_blank" rel="noopener" href="https://gitee.com/cheng_song">Gitee</a></div><hr><div class="author-info-articles"><a class="author-info-articles__archives article-meta" href="/archives"><span class="pull-left">文章</span><span class="pull-right">52</span></a><a class="author-info-articles__tags article-meta" href="/tags"><span class="pull-left">标签</span><span class="pull-right">45</span></a></div></div></div><div id="content-outer"><div id="top-container" style="background-image: url(/img/blog-bg.jpg)"><div id="page-header"><span class="pull-left"> <a id="site-name" href="/">晟松</a></span><i class="fa fa-bars toggle-menu pull-right" aria-hidden="true"></i><span class="pull-right menus">   <a class="site-page" href="/">Home</a><a class="site-page" href="/archives">Archives</a><a class="site-page" href="/tags">Tags</a><a class="site-page" href="/categories">Categories</a></span><span class="pull-right"><a class="site-page social-icon search"><i class="fa fa-search"></i><span> 搜索</span></a></span></div><div id="post-info"><div id="post-title">yarn和npm</div><div id="post-meta"><time class="post-meta__date"><i class="fa fa-calendar" aria-hidden="true"></i> 2020-09-07</time><div class="post-meta-wordcount"><span>字数总计: </span><span class="word-count">1.5k</span><span class="post-meta__separator">|</span><span>阅读时长: 5 分钟</span></div></div></div></div><div class="layout" id="content-inner"><article id="post"><div class="article-container" id="post-content"><p>对yarn和npm的一些命令的简单学习和对比，还有一些其他的可能会用的东西在里面</p>
<h5 id="1-快速删除node-modules"><a href="#1-快速删除node-modules" class="headerlink" title="1.快速删除node_modules"></a>1.快速删除node_modules</h5><p>手动删除真的很慢：</p>
<ul>
<li>安装： <code>npm install rimraf -g</code></li>
<li>使用：<code>rimraf node_modules</code></li>
</ul>
<p>实测2分钟之内就能删除，比手动快多了</p>
<h5 id="2-yarn的安装"><a href="#2-yarn的安装" class="headerlink" title="2.yarn的安装:"></a>2.yarn的安装:</h5><ol>
<li>下载node.js，使用npm安装<br><code>npm install -g yarn</code><br>`查看版本：yarn –version</li>
<li>Yarn 淘宝源安装，分别复制粘贴以下代码行到黑窗口运行即可<br>yarn config set registry <code>https://registry.npm.taobao.org -g</code><br>yarn config set sass_binary_site <code>http://cdn.npm.taobao.org/dist/node-sass -g</code></li>
</ol>
<h5 id="3-常用"><a href="#3-常用" class="headerlink" title="3.常用"></a>3.常用</h5><figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">安装yarn </span><br><span class="line">npm install -g yarn</span><br><span class="line">安装成功后，查看版本号： </span><br><span class="line">yarn --version</span><br><span class="line">创建文件夹 yarn </span><br><span class="line">md yarn</span><br><span class="line">进入yarn文件夹 </span><br><span class="line"><span class="built_in">cd</span> yarn</span><br><span class="line">初始化项目 </span><br><span class="line">yarn init // 同npm init，执行输入信息后，会生成package.json文件</span><br><span class="line">yarn的配置项： </span><br><span class="line">yarn config list // 显示所有配置项</span><br><span class="line">yarn config get &lt;key&gt; //显示某配置项</span><br><span class="line">yarn config delete &lt;key&gt; //删除某配置项</span><br><span class="line">yarn config <span class="built_in">set</span> &lt;key&gt; &lt;value&gt; [-g|--global] //设置配置项</span><br><span class="line">安装包： </span><br><span class="line">yarn install //安装package.json里所有包，并将包及它的所有依赖项保存进yarn.lock</span><br><span class="line">yarn install --flat //安装一个包的单一版本</span><br><span class="line">yarn install --force //强制重新下载所有包</span><br><span class="line">yarn install --production //只安装dependencies里的包</span><br><span class="line">yarn install --no-lockfile //不读取或生成yarn.lock</span><br><span class="line">yarn install --pure-lockfile //不生成yarn.lock</span><br><span class="line">添加包（会更新package.json和yarn.lock）：</span><br><span class="line">yarn add [package] // 在当前的项目中添加一个依赖包，会自动更新到package.json和yarn.lock文件中</span><br><span class="line">yarn add [package]@[version] // 安装指定版本，这里指的是主要版本，如果需要精确到小版本，使用-E参数</span><br><span class="line">yarn add [package]@[tag] // 安装某个tag（比如beta,next或者latest）</span><br><span class="line">//不指定依赖类型默认安装到dependencies里，你也可以指定依赖类型：</span><br><span class="line"></span><br><span class="line">yarn add --dev/-D // 加到 devDependencies</span><br><span class="line">yarn add --peer/-P // 加到 peerDependencies</span><br><span class="line">yarn add --optional/-O // 加到 optionalDependencies</span><br><span class="line">//默认安装包的主要版本里的最新版本，下面两个命令可以指定版本：</span><br><span class="line"></span><br><span class="line">yarn add --exact/-E // 安装包的精确版本。例如yarn add foo@1.2.3会接受1.9.1版，但是yarn add foo@1.2.3 --exact只会接受1.2.3版</span><br><span class="line">yarn add --tilde/-T // 安装包的次要版本里的最新版。例如yarn add foo@1.2.3 --tilde会接受1.2.9，但不接受1.3.0</span><br><span class="line">发布包</span><br><span class="line"></span><br><span class="line">yarn publish</span><br><span class="line">移除一个包 </span><br><span class="line">yarn remove &lt;packageName&gt;：移除一个包，会自动更新package.json和yarn.lock</span><br><span class="line">更新一个依赖 </span><br><span class="line">yarn upgrade 用于更新包到基于规范范围的最新版本</span><br><span class="line">运行脚本 </span><br><span class="line">yarn run 用来执行在 package.json 中 scripts 属性下定义的脚本</span><br><span class="line">显示某个包的信息 </span><br><span class="line">yarn info &lt;packageName&gt; 可以用来查看某个模块的最新版本信息</span><br><span class="line">缓存 </span><br><span class="line">yarn cache </span><br><span class="line">yarn cache list <span class="comment"># 列出已缓存的每个包 </span></span><br><span class="line">yarn cache dir <span class="comment"># 返回 全局缓存位置 </span></span><br><span class="line">yarn cache clean <span class="comment"># 清除缓存</span></span><br></pre></td></tr></table></figure>

<p><img src="https://img-blog.csdn.net/20180802111644352?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3l3MDB5dw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt="对比"></p>
<p>在vue项目里面，</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">npm run dev == yarn run dev == yarn dev</span><br><span class="line">npm run build  == yarn run build == yarn build</span><br></pre></td></tr></table></figure>



<p>这是另一个网址的记录，看着好像还行</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">1. 开始一个新工程</span><br><span class="line">yarn init 与 npm init 一样通过交互式会话创建一个 package.json</span><br><span class="line">yarn init <span class="comment"># yarn</span></span><br><span class="line"></span><br><span class="line">npm init <span class="comment"># npm</span></span><br><span class="line"><span class="comment"># 跳过会话，直接通过默认值生成 package.json</span></span><br><span class="line">yarn init --yes <span class="comment"># 简写 -y</span></span><br><span class="line"></span><br><span class="line">npm init -y</span><br><span class="line">2. 添加一个依赖</span><br><span class="line">通过 yarn add 添加依赖会更新 package.json 以及 yarn.lock 文件</span><br><span class="line">1).开发环境</span><br><span class="line">yarn add &lt;packageName&gt; 依赖会记录在 package.json 的 dependencies 下 开发环境</span><br><span class="line">yarn add webpack@2.3.3 <span class="comment"># yarn --save 是 yarn 默认的，默认记录在 package.json 中</span></span><br><span class="line"></span><br><span class="line">npm install webpack@2.3.3 --save <span class="comment"># npm</span></span><br><span class="line">2).生产环境</span><br><span class="line">yarn add &lt;packageName&gt; --dev 依赖会记录在 package.json 的 devDependencies 下 生产环境</span><br><span class="line">yarn add webpack --dev <span class="comment"># yarn 简写 -D</span></span><br><span class="line"></span><br><span class="line">npm install webpack --save-dev <span class="comment"># npm</span></span><br><span class="line">3).全局</span><br><span class="line">yarn global add &lt;packageName&gt; 全局安装依赖</span><br><span class="line">yarn global add webpack <span class="comment"># yarn</span></span><br><span class="line"></span><br><span class="line">npm install webpack -g <span class="comment"># npm</span></span><br><span class="line">3. 更新一个依赖</span><br><span class="line">yarn upgrade 用于更新包到基于规范范围的最新版本</span><br><span class="line">yarn upgrade <span class="comment"># 升级所有依赖项，不记录在 package.json 中</span></span><br><span class="line">npm update <span class="comment"># npm 可以通过 ‘--save|--save-dev’ 指定升级哪类依赖</span></span><br><span class="line"></span><br><span class="line">yarn upgrade webpack <span class="comment"># 升级指定包</span></span><br><span class="line">npm update webpack --save-dev <span class="comment"># npm</span></span><br><span class="line"></span><br><span class="line">yarn upgrade --latest <span class="comment"># 忽略版本规则，升级到最新版本，并且更新 package.json</span></span><br><span class="line">4. 移除一个依赖</span><br><span class="line">yarn remove &lt;packageName&gt;</span><br><span class="line">yarn remove webpack <span class="comment"># yarn</span></span><br><span class="line">npm uninstall webpack --save <span class="comment"># npm 可以指定 --save | --save-dev</span></span><br><span class="line">5. 安装 package.json 中的所有文件</span><br><span class="line">yarn 或者 yarn install</span><br><span class="line">yarn install <span class="comment"># 或者 yarn 在 node_modules 目录安装 package.json 中列出的所有依赖</span></span><br><span class="line">npm install <span class="comment"># npm</span></span><br><span class="line"></span><br><span class="line">yarn install 安装时，如果 node_modules 中有相应的包则不会重新下载 --force 可以强制重新下载安装</span><br><span class="line">yarn install --force <span class="comment"># 强制下载安装</span></span><br><span class="line">npm install --force <span class="comment"># npm</span></span><br><span class="line">6. 运行脚本</span><br><span class="line">yarn run 用来执行在 package.json 中 scripts 属性下定义的脚本</span><br><span class="line">// package.json</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;scripts&quot;</span>: &#123;</span><br><span class="line">    <span class="string">&quot;dev&quot;</span>: <span class="string">&quot;node app.js&quot;</span>,</span><br><span class="line">    <span class="string">&quot;start&quot;</span>: <span class="string">&quot;node app.js&quot;</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">yarn run dev <span class="comment"># yarn 执行 dev 对应的脚本 node app.js</span></span><br><span class="line">npm run <span class="comment"># npm</span></span><br><span class="line"></span><br><span class="line">yarn start <span class="comment"># yarn</span></span><br><span class="line">npm start <span class="comment"># npm</span></span><br><span class="line">与 npm 一样 可以有 yarn start 和 yarn <span class="built_in">test</span> 两个简写的运行脚本方式</span><br><span class="line">7. 显示某个包信息</span><br><span class="line">yarn info &lt;packageName&gt; 可以用来查看某个模块的最新版本信息</span><br><span class="line">yarn info webpack <span class="comment"># yarn</span></span><br><span class="line">npm info webpack <span class="comment"># npm</span></span><br><span class="line"></span><br><span class="line">yarn info webpack --json <span class="comment"># 输出 json 格式</span></span><br><span class="line">npm info webpack --json <span class="comment"># npm</span></span><br><span class="line"></span><br><span class="line">yarn info webpack readme <span class="comment"># 输出 README 部分</span></span><br><span class="line">npm info webpack readme</span><br><span class="line">8. 列出项目的所有依赖</span><br><span class="line">yarn list</span><br><span class="line">yarn list <span class="comment"># 列出当前项目的依赖</span></span><br><span class="line">npm list <span class="comment"># npm</span></span><br><span class="line"></span><br><span class="line">yarn list --depth=0 <span class="comment"># 限制依赖的深度</span></span><br><span class="line">sudo yarn global list <span class="comment"># 列出全局安装的模块</span></span><br><span class="line">9. 管理 yarn 配置文件</span><br><span class="line">yarn coinfig</span><br><span class="line">yarn config <span class="built_in">set</span> key value <span class="comment"># 设置</span></span><br><span class="line">npm config <span class="built_in">set</span> key value</span><br><span class="line"></span><br><span class="line">yarn config get key <span class="comment"># 读取值</span></span><br><span class="line">npm config get key</span><br><span class="line"></span><br><span class="line">yarn config delete key <span class="comment"># 删除</span></span><br><span class="line">npm config delete key</span><br><span class="line"></span><br><span class="line">yarn config list <span class="comment"># 显示当前配置</span></span><br><span class="line">npm config list</span><br><span class="line"></span><br><span class="line">yarn config <span class="built_in">set</span> registry https://registry.npm.taobao.org <span class="comment"># 设置淘宝镜像</span></span><br><span class="line">npm config <span class="built_in">set</span> registry https://registry.npm.taobao.org <span class="comment"># npm</span></span><br><span class="line">10. 缓存</span><br><span class="line">yarn cache</span><br><span class="line">sudo yarn cache list <span class="comment"># 列出已缓存的每个包</span></span><br><span class="line">sudo yarn cache dir <span class="comment"># 返回 全局缓存位置</span></span><br><span class="line">sudo yarn cache clean <span class="comment"># 清除缓存</span></span><br></pre></td></tr></table></figure>

</div></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">晟松</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://chengsong.info/2020/09/07/yarn和npm/">https://chengsong.info/2020/09/07/yarn和npm/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://chengsong.info">晟松</a>！</span></div></div><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/yarn-npm/">yarn npm</a></div><nav id="pagination"><div class="prev-post pull-left"><a href="/2020/09/07/webpack%E7%AE%80%E5%8D%95%E4%BD%BF%E7%94%A8/"><i class="fa fa-chevron-left">  </i><span>webpack简单使用</span></a></div><div class="next-post pull-right"><a href="/2020/08/25/js%20promise%20prototype%E5%A4%8D%E4%B9%A0/"><span>js promise复习</span><i class="fa fa-chevron-right"></i></a></div></nav></div></div><footer class="footer-bg" style="background-image: url(/img/blog-bg.jpg)"><div class="layout" id="footer"><div class="copyright">&copy;2020 - 2023 By 晟松</div><div class="framework-info"><span>驱动 - </span><a target="_blank" rel="noopener" href="http://hexo.io"><span>Hexo</span></a><span class="footer-separator">|</span><span>主题 - </span><a target="_blank" rel="noopener" href="https://github.com/Molunerfinn/hexo-theme-melody"><span>Melody</span></a></div><div class="icp"><a target="_blank" rel="noopener" href="http://www.beian.miit.gov.cn/"><span>湘ICP备2020021729号</span></a></div><div class="busuanzi"><script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><span id="busuanzi_container_page_pv"><i class="fa fa-file"></i><span id="busuanzi_value_page_pv"></span><span></span></span></div></div></footer><i class="fa fa-arrow-up" id="go-up" aria-hidden="true"></i><script src="https://cdn.jsdelivr.net/npm/animejs@latest/anime.min.js"></script><script src="https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@latest/velocity.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-ui-pack@latest/velocity.ui.min.js"></script><script src="/js/utils.js?version=1.9.0"></script><script src="/js/fancybox.js?version=1.9.0"></script><script src="/js/sidebar.js?version=1.9.0"></script><script src="/js/copy.js?version=1.9.0"></script><script src="/js/fireworks.js?version=1.9.0"></script><script src="/js/transition.js?version=1.9.0"></script><script src="/js/scroll.js?version=1.9.0"></script><script src="/js/head.js?version=1.9.0"></script><script src="/js/search/local-search.js"></script><script>if(/Android|webOS|iPhone|iPod|iPad|BlackBerry/i.test(navigator.userAgent)) {
  $('#nav').addClass('is-mobile')
  $('footer').addClass('is-mobile')
  $('#top-container').addClass('is-mobile')
}</script><div class="search-dialog" id="local-search"><div class="search-dialog__title" id="local-search-title">本地搜索</div><div id="local-input-panel"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章"></div></div></div><hr><div id="local-search-results"><div id="local-hits"></div><div id="local-stats"><div class="local-search-stats__hr" id="hr"><span>由</span> <a target="_blank" rel="noopener" href="https://github.com/wzpan/hexo-generator-search" style="color:#49B1F5;">hexo-generator-search</a>
 <span>提供支持</span></div></div></div><span class="search-close-button"><i class="fa fa-times"></i></span></div><div class="search-mask"></div></body></html>